package com.koicarp.university.graduate.auth.controller;

import com.koicarp.university.graudate.common.dto.SysUserDto;
import com.koicarp.university.graduate.auth.manager.UserManager;
import com.koicarp.university.graduate.auth.service.ISysUserService;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Map;

/**
 * @auther liutao
 * @Date 2020-08-17 16:53:07
 */
@Slf4j
@RestController
public class UniversityController {
    @Autowired
    private ConsumerTokenServices consumerTokenServices;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private UserManager userManager;
    @GetMapping("oauth/test")
    public String testOauth() {
        return "oauth";
    }


//    @PostMapping("loginOut")
//    public UniversityResponse loginOut(HttpServletRequest request) throws Exception {
//        String authorization = request.getHeader("Authorization");
//        String token = StringUtils.replace(authorization, "Bearer ", "");
//        System.out.println(token);
//        UniversityResponse response = new UniversityResponse();
//        if (!consumerTokenServices.revokeToken(token)) {
//            log.error("退出登录失败！！！");
//            throw new Exception("退出登录失败");
//        }
//        response.code(BaseConstant.USER_LOGOUT_SUCESS_8888);
//        response.message("用户登出成功！！！");
//        return response;
//    }
//    @PostMapping("/oauth/login")
//    public UniversityResponse login(@RequestParam Map<String,Object> map){
//        MultiValueMap<String,Object> paramsMap=new LinkedMultiValueMap<>();
//        if(map.get("grant_type").equals("password")){
//            paramsMap.set("username",map.get("username"));
//            paramsMap.set("password",map.get("password"));
//        }else if(map.get("grant_type").equals("refresh_token")){
//            paramsMap.set("refresh_token",map.get("refresh_token"));
//        }
//        paramsMap.set("grant_type",map.get("grant_type"));
//        RestTemplate restTemplate=new RestTemplate();
//        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(map.get("client_id").toString(),map.get("client_secret").toString()));
//        OAuth2AccessToken token=restTemplate.postForObject("http://localhost:8080/koicarp/oauth/token",paramsMap,OAuth2AccessToken.class);
//        UniversityResponse response = new UniversityResponse();
//        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
//        response.message(BaseConstant.LOGIN_USER_SUCCESS);
//        response.data(token);
//        return response;
//    }
    @PostMapping("/user/findMenuByRole")
    public UniversityResponse findMenuByRole(Principal principal){
        String name = principal.getName();
        SysUserDto user = sysUserService.getUserByName(name);
        UniversityResponse response=new UniversityResponse();
        if (user==null){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.USER_ACCOUNT_NOT_EXIST);
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.data(userManager.findMenuByRole(user.getRoleIds()));
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
       return response;
    }

}
